package controllers;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import model.ComplaintReportModel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

import view.ViewWeeklyGUI;
import client.CliMessage;




/**
 * The Class ViewWeeklyController that extends AbstractController implements IValidateInput.
 * @author yudku
 */
public class ViewWeeklyController extends AbstractController implements IValidateInput
{	
	
	/** The Weekly view. */
	private ViewWeeklyGUI WeeklyView;
	
	/** The ComplaintReportModel report model. */
	private ComplaintReportModel CompReportModel;
	private static int flag = 0;
	
	/**
	 * This is ViewWeeklyController controller constructor.
	 *
	 * @param WeeklyView the weekly view
	 * @param CompReportModel the comp report model
	 */
	
	public ViewWeeklyController(ViewWeeklyGUI WeeklyView, ComplaintReportModel CompReportModel) 
	{
		super(WeeklyView,CompReportModel);
		this.WeeklyView = WeeklyView;
		this.CompReportModel = CompReportModel;
		
		this.WeeklyView.addButtonListener1(new Button1Listener());
		this.WeeklyView.addButtonListener2(new Button2Listener());
	}
	
	@Override
	public boolean validateInput() 
	{
		return true;		
	}
	
	class Button1Listener extends AbstractListener
	{
		public Button1Listener ()
		{	
		}

		@Override
		public void actionPerformed(ActionEvent arg0) 
		{		
			String searchQuery = "search"	+ "SELECT * "
                                            + "FROM daily_statistc";
			
			ViewWeeklyController.flag = 1;			                 
            sendToServer(searchQuery);				                       											
		}
	}
	
	class Button2Listener extends AbstractListener
	{
		public Button2Listener ()
		{	
		}

		@Override
		public void actionPerformed(ActionEvent arg0) 
		{		
			String searchQuery = "search"	+ "SELECT * "
                                            + "FROM daily_statistc";
			
			ViewWeeklyController.flag = 2;						                 
            sendToServer(searchQuery);				                       											
		}
	}

	@Override
	public void handleDBRequestSuccess()
	{
		WeeklyView.displayWrnMessage("Reply Succesfully Enterd");		
	}

	@Override
	public void handleDBSearchResult(CliMessage dbResult)
	{	
		
		int[] WeeklyArrayDown = new int [7];
		for(int i = 0 ; i < 7  ; i++)
			WeeklyArrayDown[i] = 0;
		int[] WeeklyArrayUp = new int [7];
		for(int i = 0 ; i < 7  ; i++)
			WeeklyArrayUp[i] = 0;
		
		Calendar cal = Calendar.getInstance();	
		int currMonth = cal.get(Calendar.MONTH);
		currMonth++;
		int currYear = cal.get(Calendar.YEAR);
		int day = cal.get(Calendar.DAY_OF_MONTH);
		
		ArrayList<HashMap<String, String>> srs = dbResult.getSrs();
		Map<String, String> weeklyTupple  = srs.get(srs.size() -1);  
		
		if  (weeklyTupple.get("MEDIANDOWN") != null)
		{
			String medDown = weeklyTupple.get("MEDIANDOWN");
			String standDown = weeklyTupple.get("STANDARDDOWN");
			String medAdd = weeklyTupple.get("MEDIANADD");
			String standAdd = weeklyTupple.get("STANDARDADD");
			String DATE = weeklyTupple.get("DATE").substring(0,16);

			///set fields GUi
			WeeklyView.SetGenLabel(medDown,  WeeklyView.getlabel1_2());
			WeeklyView.SetGenLabel(standDown,  WeeklyView.getlabel2_2());
			WeeklyView.SetGenLabel(medAdd,  WeeklyView.getlabel4_2());
			WeeklyView.SetGenLabel(standAdd,  WeeklyView.getlabel5_2());
			WeeklyView.SetGenLabel(DATE,  WeeklyView.date_Label());
		}
		
		if  (weeklyTupple.get("NEWPHOTO") != null)
		{

			   
			   	for(int i = 0 ; i < srs.size()  ; i++)
			   	{
			   	      Map<String, String> Tupple  = srs.get(i);	
			   	      
			 		  int DBmonth = (Integer.parseInt((Tupple.get("DATE")).substring(5, 7)));
					  int DByear = (Integer.parseInt((Tupple.get("DATE")).substring(0, 4)));
					  int DBday = (Integer.parseInt((Tupple.get("DATE")).substring(8, 10)));
			   	      
			   	      if ((currMonth == DBmonth) && (currYear == DByear) && (DBday <= day) && (DBday > (day - 7)))
			   	      {
			   	    	  WeeklyArrayDown [ (day - DBday) % 7 ] = Integer.parseInt((Tupple.get("DOWNLOADED")));
			   	    	  WeeklyArrayUp [ (day - DBday) % 7 ] = Integer.parseInt((Tupple.get("NEWPHOTO")));
			   	      }	
			   	}
			   
			   	for (int i = 0; i<7; i++)
			   		System.out.println (WeeklyArrayDown[i]+ " **** ");
			
	 		  if (flag == 1)
	 		  {
	 			  DefaultCategoryDataset dataset = new DefaultCategoryDataset();
	 			  dataset.setValue(WeeklyArrayDown[0] ,"Downloaded" , "Sunday" );
	 			  dataset.setValue(WeeklyArrayDown[1] ,"Downloaded" , "Monday" );
	 			  dataset.setValue(WeeklyArrayDown[2] ,"Downloaded" , "Tuesday" );
	 			  dataset.setValue(WeeklyArrayDown[3] ,"Downloaded" , "Wednesday" );
	 			  dataset.setValue(WeeklyArrayDown[4] ,"Downloaded" , "Thursday" );
	 			  dataset.setValue(WeeklyArrayDown[5] ,"Downloaded" , "Friday" );
	 			  dataset.setValue(WeeklyArrayDown[6] ,"Downloaded" , "Saturday" );
	 			  JFreeChart chart = ChartFactory.createBarChart("DOWNLOADED PHOTO", "Day", "Downloaded", dataset, PlotOrientation.VERTICAL, false, true, false);
	 			  CategoryPlot p = chart.getCategoryPlot();
	 			  p.setRangeGridlinePaint(Color.black);
	 			  ChartFrame frame = new ChartFrame ("Frequency distribution download", chart);
	 			  frame.setVisible(true);
	 			  frame.setSize (700,700);
	 		  }
	 		  else
	 		  {
	 			 DefaultCategoryDataset dataset = new DefaultCategoryDataset();
	 	 		 dataset.setValue(WeeklyArrayUp[0] ,"UPLOADED" , "Sunday" );
		 		 dataset.setValue(WeeklyArrayUp[1] ,"UPLOADED" , "Monday" );
		 		 dataset.setValue(WeeklyArrayUp[2] ,"UPLOADED" , "Tuesday" );
		 		 dataset.setValue(WeeklyArrayUp[3] ,"UPLOADED" , "Wednesday" );
		 		 dataset.setValue(WeeklyArrayUp[4] ,"UPLOADED" , "Thursday" );
		 		 dataset.setValue(WeeklyArrayUp[5] ,"UPLOADED" , "Friday" );
		 		 dataset.setValue(WeeklyArrayUp[6] ,"UPLOADED" , "Saturday" );
	 			 JFreeChart chart = ChartFactory.createBarChart("Uploaded PHOTO", "Day", "Uploaded", dataset, PlotOrientation.VERTICAL, false, true, false);
	 			 CategoryPlot p = chart.getCategoryPlot();
	 			 p.setRangeGridlinePaint(Color.black);
	 			 ChartFrame frame = new ChartFrame ("Frequency distribution upload", chart);
	 			 frame.setVisible(true);
	 			 frame.setSize (700,700);
	 		  }
		}

		ViewWeeklyController.flag = 0;
		//display main window
		WeeklyView.setVisible(true);						
	}

	@Override
	public void handleNullSearchResult() 
	{
		WeeklyView.displayWrnMessage("DATA DOES NOT EXIST!");
		
	}

}
